package producersConsumersTests.WaitingTime;

import java.util.LinkedList;
import java.util.List;

import prod_cons.TaskContainer;
import producersConsumersTests.WaitingTime.TimedTask.WhenToMeasure;
import producersConsumersTests.utils.Remover;


/**
 * TimeSettingRemover is a remover that saves the exit time of the task, and the
 * task itself in a list.
 * 
 */
public class TimeSettingRemover extends Remover {
	private List<TimedTask> measures = new LinkedList<TimedTask>();
	private final WhenToMeasure when;
	private long measure;
	
	public TimeSettingRemover(TaskContainer container, WhenToMeasure when) {
		super(container);
		this.when = when;
	}

	@Override
	public void beforeGetAttempt() {
		if (when == WhenToMeasure.BEFORE)
			this.measure = System.nanoTime();
	}
	
	@Override
	public void afterGetAttempt(Runnable task) {
		if (task == null)
			return;

		if (when == WhenToMeasure.AFTER)
			this.measure = System.nanoTime();
		TimedTask timed = (TimedTask) task;
		timed.setExitTime(measure);
		measures.add(timed);
	}

	public List<TimedTask> getMeasures() {
		return measures;
	}
}
